package net.sudot.chess.business.model;

import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Transient;
import java.math.BigDecimal;

/**
 * 游戏订单
 *
 * @author tangjialin on 2018-03-21.
 */
@Getter
@Setter
@Accessors(chain = true)
@Entity
public class OrderGame extends BaseOrder<OrderGame> {
    /** 状态 */
    public enum Status {
        /** */ PAID("已报名"),
        /** */ COMPLETED("已结束"),
        /** */ CANCELED("已退出");
        private String memo;

        Status(String memo) {
            this.memo = memo;
        }

        public String memo() {
            return memo;
        }
    }

    /** 游戏主键 */
    private Long gameId;
    /** 状态 */
    @Enumerated(EnumType.STRING)
    private OrderGame.Status status;
    /** 退款金额 */
    private BigDecimal refundAmount;

    /**
     * 获取应付金额
     *
     * @return 应付金额
     */
    @Override
    @Transient
    public BigDecimal getPayableAmount() {
        if (hasExpired() || getStatus().ordinal() >= OrderGame.Status.COMPLETED.ordinal()) {
            return BigDecimal.ZERO;
        }
        BigDecimal amountPayable = getPaymentAmount().subtract(getPaidAmount());
        return amountPayable.compareTo(BigDecimal.ZERO) > 0 ? amountPayable : BigDecimal.ZERO;
    }

    /**
     * 获取应退金额
     *
     * @return 应退金额
     */
    @Override
    @Transient
    public BigDecimal getRefundableAmount() {
        if (hasExpired() || getStatus().ordinal() > Status.COMPLETED.ordinal()) {
            BigDecimal refundableAmount = getPaidAmount();
            return refundableAmount.compareTo(BigDecimal.ZERO) >= 0 ? refundableAmount : BigDecimal.ZERO;
        }
        if (OrderGame.Status.COMPLETED.equals(getStatus())) {
            BigDecimal refundableAmount = getPaidAmount().subtract(getPaymentAmount());
            return refundableAmount.compareTo(BigDecimal.ZERO) >= 0 ? refundableAmount : BigDecimal.ZERO;
        }
        return BigDecimal.ZERO;
    }

    /**
     * 游戏订单永不过期
     *
     * @return true:已过期,false:未过期
     */
    @Override
    @Transient
    public boolean hasExpired() {
        return false;
    }
}